from django.conf.urls.defaults import *
from blog.views import LatestPost
from django.contrib.sitemaps import Sitemap, FlatPageSitemap, GenericSitemap
from blog.models import *
from music.models import *
from gig.views import GigsFeed
from blog.views import artists, artist_posts
import bug_view
from django.contrib.comments.signals import comment_was_posted
from django.http import HttpResponseRedirect
from django.core.mail import send_mail
import settings




def on_comment_was_posted(sender, comment, request, *args, **kwargs):
    subject = 'new comment'
    domain = comment.site.domain
    ct = comment.content_type
    ct_object = ct.get_object_for_this_type(pk=comment.object_pk) 
    url_read = domain + ct_object.get_absolute_url()
    content = 'new comment : %s' % (url_read)
    send_mail(subject, content, 'contact@sam-lesite.fr',['samusic@free.fr'], fail_silently=False)

comment_was_posted.connect(on_comment_was_posted)

from django.contrib import admin
admin.autodiscover()

class BaseSitemap(Sitemap):
    priority = 0.8
    def items(self):
        return ['/', '/blog/', '/gig/', '/link/', '/album/']
    def location(self, obj):
        return obj

feeds = {
    'latest': LatestPost,
    'concerts': GigsFeed,
}

blog_sitemap_dict = {
    'queryset': Post.objects.all(),
    'date_field': 'date',
}

sitemaps = {
    'flatpages': FlatPageSitemap,
    'blog': GenericSitemap(blog_sitemap_dict),
    'list': BaseSitemap(), 
    'album': AlbumSitemap(),
    'song': SongSitemap(),
}

from django.core.urlresolvers import reverse

urlpatterns = patterns('',
    url(r'^contact/', include('contact_form.urls')),
    url(r'^blog/', include('blog.urls')),
    url(r'^map/', artists, name="map"),
    url(r'^ref/(?P<object_id>\d+)/$', artist_posts, name="artist-posts"),
    url(r'^gig/', include('gig.urls')),
    url(r'^link/$', include('link.urls')),
    url(r'^music/', include('music.urls')),
    url(r'^multimedia/', include('multimedia.urls')),
    url(r'^bug_view/', bug_view.debug),
    (r'^tinymce/', include('tinymce.urls')),
    (r'^comments/', include('django.contrib.comments.urls')),
    (r'^i18n/', include('django.conf.urls.i18n')),
    (r'^admin/', include(admin.site.urls)),
    (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feeds}),
    (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
)

urlpatterns += patterns('django.views.generic.simple',
    ('^$', 'redirect_to', {'url': reverse('blog-list')}),
)

urlpatterns += patterns('',
    url(r'^rosetta/', include('rosetta.urls')),
)

from settings import MEDIA_ROOT

urlpatterns += patterns('',
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': MEDIA_ROOT, 'show_indexes': True}),
)


